<?php

class PayTool {
    var $return_url = "http://www.baidu.com";

   // var $merchantId = "1000022215230183";
   // var $pay_key = "VSV2IZF1DJ3BY9NICZWZ29JNK9347CF6";
   // var $des_key = "VSV2IZF1DJ3BY9NICZWZ29JNK9347CF6";
   // var $req_url = "http://39.108.125.31:8170/BusiM/AL001/Check";
    // var $req_url = "http://39.108.125.31:8170/BusiM/AL001/Check";
    var $merchantId = "1000021715142501";
   var $pay_key = "XVL8U0PFHLYHJPYTGK2PLC17EX907KBF";
   var $des_key = "XVL8U0PFHLYHJPYTGK2PLC17EX907KBF";
   var $req_url = "http://120.78.214.185:6170/BusiM/AL001/Check";
   var $callBack_url = "http://120.78.214.185:6170/BusiM/AL002/TestUserQRCallBackServlet";

    function sign ($params, $key){
        foreach ($params as $_key=>$_value) {
            if(trim($_value)=='' || strcmp($_key, 'sign') == 0) {
                unset($params[$_key]);
            }
        }
        ksort($params);
        $paramsStr = $this->paramsToStr($params);
        $contentStr = $paramsStr."&key=".$key;
        $result = md5($contentStr);
//        echo '<br>content ： '.$contentStr.'<br>'.'md5 : '.$result.'<br><br>';
        return $result;
    }
    function paramsToStr($params){
        $paramsStr = '';
        foreach($params as $key=>$value){
            $paramsStr = $paramsStr .$key."=".$value."&";
        }
        $paramsStr = substr($paramsStr, 0 ,strlen($paramsStr)-1);
        return $paramsStr;
    }
    function object2array($object) {
        $array =  json_decode( json_encode( $object),true);
        return  $array;
    }
    //不嵌套的object
    function object2Str($object){
        $objectArr = $this->object2array($object);
        $str = $this->paramsToStr($objectArr);
        return $str;
    }
    function getRandomStr( $length) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $randomStr = '';
        for ( $i = 0; $i < $length; $i++ )
        {
            $randomStr .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        return $randomStr;
    }
    function getNoCheckIPSN($merchant){
        $randomStr = $this->getRandomStr(12);
        $header = $randomStr;
        for($i=1; $i<5; $i++){
            $header = $header.$merchant;
            $header = md5($header);
            $header = substr($header,0 ,8);
        }
        return 'Ts'.$randomStr.$header;
    }

    function getSN($merchant){
        return $this->getNoCheckIPSN($merchant);
    }

    function getResultWithUrl($url, $params){
        $curlPost = $this->paramsToStr($params);
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    function checkResult($result){
        $response = json_decode($result);
        if (isset($response->sign)) {
            $sign = $response->sign;
            $response = $this->object2array($response);
            $reSign = $this->sign($response, $this->pay_key);
            $reSign = strtoupper($reSign);
            return strcmp($sign, $reSign) == 0 ? true : false;
        } else {
            echo "签名不存在";
        }
        return false;
    }
    function stringEqual($origin, $compare){
        return strcmp($origin, $compare) == 0 ? true : false;
    }
    function resultSuccess($result){
        $success = $this->stringEqual($result->code, '0000');
        $success = $success || $this->stringEqual($result->code, '8001');
        return $success;
    }
}

?>